<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/23 0023
 * Time: 15:15
 */

namespace app\admin\controller;
use think\Db;
use think\Controller;


class Aos extends Bases
{

    public function _initialize(){
        if(!session('id')){
            $this->redirect('/admin/index/index');
        }
    }

    public function cg(){
        if(request()->isPost()){
            $is_recom = intval(input('param.is_recom'));
            $class_id = intval(input('param.class_id'));
            if(isset($class_id) && $class_id !==''){
                $where['is_recom'] = $is_recom;
            }
            if(!empty($class_id)){
                $where['class_id'] = $class_id;
            }
        }
        $where['is_del']=0;
        $where['sign']="pc";
        $data['list'] = Db::name('aos')->where($where)->order('sort desc,id desc')->select();
        $data['info']=Db::name('aosclass')->select();
        $this->assign('data',$data);

        return $this->fetch();
    }
    //按钮状态
    public function start(){
//        $is_recom = (int)input('post.is_recom');
        $id = (int)input('post.id');
        if($id){
            $info=Db::name('aos')->where('id',$id)->find();
            if ($info['is_recom']==1){
                $data['is_recom']=0;
            }else{
                $data['is_recom']=1;
            }
            $result = Db::name('aos')->where('id',$id)->update($data);
            if($result){
                return json(['code'=>1,'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            return json(['code'=>1,'msg'=>'数据异常']);
        }
    }
    public function add_cg(){
        $info=Db::name('aosclass')->where('show',1)->select();
        $this->assign('info',$info);
        return $this->fetch();
    }

    public function edit_cg(){
        $id = input('param.id');
        if($id){
            $data =Db::name('aos')->where('id',$id)->find();
            $data['info']=Db::name('aosclass')->where('show',1)->select();
            $works_pic = Db('works_pic')->where('pic_id','in',$data['works_photo'])->field('works_pic,pic_id')->select();
            $data['content'] = html_entity_decode($data['content']);
            $this->assign('data',$data);
            $this->assign('imgurl',$works_pic);
        }
        return $this->fetch();
    }

    //分类管理
    public function vclass(){
//            $where['id']=0;
//            $where['sign']="pc";
        $data = Db::name('aosclass')->where('show',1)->order('class_id desc')->select();
        $this->assign('data',$data);

        return $this->fetch();
    }
//删除
    public function jddel(){
        $id = intval(input('post.id'));
        if($id){
            $res = Db::name('aosclass')->where('class_id',$id)->delete();
        }
        if($res){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }

    public function add_vclass(){
        $id= intval(input('param.id'));
        if(request()->isAjax()){
            $data=[
                'name'=>input('param.name'),
                'show'=>intval(input('param.show'))
            ];
            $id= intval(input('param.id'));
            if($id> 0){
                $result = Db::name('aosclass')->where('id',$id)->update($data);
            }else{
                $result = Db::name('aosclass')->insert($data);
            }
            if($result !==false){
                return json(['code'=>1,'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }

        }
        if($id){
            $data = Db::name('aosclass')->where('class_id',$id)->find();
            $this->assign('data',$data);
        }
        return $this->fetch();
    }
    //确认提交
    public function addcg(){
        $id = input('post.id');
        $array_works_pic= isset($_POST['works_pic'])?implode(',',$_POST['works_pic']):'';
        $data=[
            'title'=>input('post.title'),
            'intro'=>input('post.intro'),
            'is_show'=>intval(input('post.is_show')),
            'class_id'=>intval(input('post.class_id')),
            'is_recom'=>intval(input('post.is_recom')),
            'Scenery'=>intval(input('post.Scenery')),
            'sort'=>intval(input('post.sort')),
            'works_photo'=>$array_works_pic,
            'content'=>input('post.content'),
            'click'=>intval(input('post.click')),
            'seo_title'=>input('param.seo_title'),
            'seo_keywords'=>input('param.seo_keywords'),
            'seo_description'=>input('param.seo_description'),
            'sign'=>"pc",
        ];
//        var_dump($data);die;
        $file_main_img = request()->file('main_img');
        $file_vice_img = request()->file('vice_img');
        if($file_main_img){
            $file_info_main_img = $file_main_img->move('./uploads/cg/');
            if($file_info_main_img){
                $data['main_img'] = '/uploads/cg/'.$file_info_main_img->getSaveName();
            }else{
                return json(['code'=>0,'msg'=>$file_main_img->getError()]);
            }
        }
        if($file_vice_img){
            $file_info_vice_img = $file_vice_img->move('./uploads/cg/');
            if($file_info_vice_img){
                $data['vice_img'] = '/uploads/cg/'.$file_info_vice_img->getSaveName();
            }else{
                return json(['code'=>0,'msg'=>$file_vice_img->getError()]);
            }
        }
        if($id){
            if($file_main_img){
                $old_img = Db::name('aos')->where('id',$id)->value('main_img');
                if($old_img){
//                    unlink('.'.str_replace('\\','/',$old_img));
                }else{

                }
            }
            $data['update_time']=time();
//            var_dump($data);die;
            $result = DB::name('aos')->where('id',$id)->update($data);
        }else{
            $data['create_time']=time();
            $result = Db::name('aos')->insert($data);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }

    //删除
    public function cgdel(){
        $id = intval(input('post.id'));
        if($id){
            $res = Db::name('aos')->where('id',$id)->update(['is_del'=>1]);;
        }
        if($res){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }
    //改变排序
    public function ChangecgSort(){
        $sort = (int)input('post.sort');
        $id = (int)input('post.id');
        if($id){
            $data['sort']=$sort;
            $result = Db::name('aos')->where('id',$id)->update($data);
            if($result){
                return json(['code'=>1,'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            return json(['code'=>1,'msg'=>'数据异常']);
        }

    }




}